package com.jrx.anytxn.transaction.service;


import com.jrx.anytxn.common.exception.TxnBizException;
import com.jrx.anytxn.common.exception.TxnException;
import com.jrx.anytxn.transaction.bean.AfterSucSaveBean;
import com.jrx.anytxn.transaction.bean.CancelSaveBean;
import com.jrx.anytxn.transaction.bean.LoanParamBean;
import com.jrx.anytxn.transaction.dto.common.DisbursementLogRes;
import com.jrx.anytxn.transaction.dto.common.RepaymentPlanInfoRes;
import com.jrx.anytxn.transaction.dto.loan.LoanConfirmReq;
import com.jrx.anytxn.transaction.dto.loan.LoanConfirmRes;
import com.jrx.anytxn.transaction.dto.loan.LoanListQueryReq;
import com.jrx.anytxn.transaction.dto.loan.LoanListQueryRes;
import com.jrx.anytxn.transaction.dto.loan.LoanRecordRes;
import com.jrx.anytxn.transaction.dto.loan.LoanReq;
import com.jrx.anytxn.transaction.entity.AmLoanAccountInfo;
import com.jrx.anytxn.transaction.entity.AmLoanMainInfo;
import com.jrx.anytxn.transaction.entity.AmRepaymentPlanInfo;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @author  haopu.zheng
 *  贷款service
 * date: 2019/7/23
**/
public interface ILoanService {

    /**
     * 生成订单相关信息 贷款订单+账户信息+还款计划+流水
     *
     * @author  zhenghaopu
     * date: 2019/9/25 9:58
     * @param loanParamBean 贷款请求参数bean
     * @param loanConfirmReq 贷款确认请求
     */
    LoanConfirmRes generatingOrderRelationInfo(LoanParamBean loanParamBean, LoanConfirmReq loanConfirmReq ) throws Exception;

    /**
     * 获取贷款信息和还款计划列表
     *
     * @param loanReq        贷款请求实体
     * @param loanParamBean  贷款请求参数
     * @author fan.yang
     * date: 2019/9/25 0025 上午 10:58
     */
    LoanConfirmRes getLoanInfoAndPaymentPlanList(LoanReq loanReq, LoanParamBean loanParamBean) throws TxnException;


    /**
     * 根据订单ID，账户状态，账户类型查询账户信息，返回账户该类型的已还金额之总和
     *
     * @param loanId 贷款ID
     * @param type   账户类型集合
     * @param cusId 客户id
     * @param tenantId 租户ID
     * @return java.math.BigDecimal
     * @author fan.yang
     * date: 2019/9/24 0024 上午 11:32
     */
    BigDecimal getPaidAmountByLoanIdAndStatusAndType(String loanId,String[] type, String cusId, String tenantId);

    /**
     * 根据渠道客户号查询订单列表
     * @author  zhenghaopu
     * date: 2019/9/25 10:00
     * @param channel 渠道
     * @param customerId 客户号
     * @param tenantId 租户ID
     * @return java.util.List<com.jrx.anytxn.transaction.dto.loan.LoanRecordRes>
     */
    List<LoanRecordRes> getLoanMainByCondition(String channel, String customerId, String tenantId) throws TxnBizException;

    /**
     * 根据订单ID，渠道号，客户ID查询放款流水表信息
     *
     * @param loanId     订单ID
     * @param customerId 客户ID
     * @param tenantId 租户ID
     * @return com.jrx.anytxn.transaction.dto.common.DisbursementLogRes
     * @author fan.yang
     * date: 2019/9/24 0024 上午 11:31
     */
    DisbursementLogRes getDisbursementByCondition(String loanId, String customerId, String tenantId) throws TxnBizException;

    /**
     * 根据订单ID，客户ID查询该订单的还款计划列表信息
     *
     * @param loanId     订单ID
     * @param customerId 客户ID
     * @param tenantId 租户ID
     * @return java.util.List<com.jrx.anytxn.transaction.dto.common.RepaymentPlanInfoRes>
     * @author fan.yang
     * date: 2019/9/24 0024 上午 11:31
     */
    List<RepaymentPlanInfoRes> getRepaymentPlanListByLoanId(String loanId, String customerId,String tenantId);


    /**
     * 查询正常状态下的账户中某订单号在某期的某种账户类型的入账金额
     *
     * @param loanId 订单ID
     * @param tenor  贷款期数
     * @param type   账户类型
     * @param customerId 客户ID
     * @param tenantId 租户ID
     * @return java.math.BigDecimal
     * @author fan.yang
     * date: 2019/9/24 0024 上午 11:31
     */
    BigDecimal getAmountByAccountMain(String loanId,Integer tenor,String type, String customerId, String tenantId);

    /**
     * 构建返回信息
     * @param userId
     * @param amLoanMainInfo
     * @param planInfoList
     * @param loanStatus
     * @return
     */
    LoanConfirmRes buildRes(String userId, AmLoanMainInfo amLoanMainInfo, List<AmRepaymentPlanInfo> planInfoList, String loanStatus) throws TxnBizException;

    /**
     * 取消订单
     * @param loanId
     * @return
     */
    CancelSaveBean cancelOrder(String loanId, String cusotmerId, String tenantId, Date businessDate, String changeLimit) throws TxnBizException;

    /**
     * 到账日起息，到账成功后处理
     * @param amLoanMainInfo
     * @param amLoanAccountInfo
     * @param businessDate
     * @return
     * @throws TxnException
     */
    AfterSucSaveBean afterLoanSuccess(AmLoanMainInfo amLoanMainInfo, AmLoanAccountInfo amLoanAccountInfo, Date businessDate) throws TxnException;

    /**
     * 对订单状态进行校验
     * @param amLoanAccountInfo
     * @throws TxnException
     */
    void checkLoanStatus(AmLoanAccountInfo amLoanAccountInfo)throws TxnException;

    /**
     * 账单分期生成订单相关信息 贷款订单+账户信息+还款计划
     *
     * @param loanParamBean 贷款请求参数bean
     */
    LoanConfirmRes generatingOrderByLoanStaging(LoanParamBean loanParamBean, LoanConfirmReq loanConfirmReq ) throws TxnException;

    /**
     * 根据规则获取账单日
     * @param orgId
     * @param proId
     * @param loanDate
     * @param tenantId
     * @return
     * @throws TxnBizException
     */
    Integer getStatementDayByRule(String orgId, String proId, Date loanDate, String tenantId) throws TxnBizException;

    /**
     * 保存取消订单数据
     * @param cancelSaveBean
     */
    void saveCancleBean(CancelSaveBean cancelSaveBean);

    /**
     * 根据规则获取还款日
     * @param orgId
     * @param proId
     * @param loanDate
     * @param tenantId
     * @return
     * @throws TxnBizException
     */
    Integer getPaymentDayByRule(String orgId, String proId, Date loanDate, String tenantId) throws TxnBizException;

    /**
     * 根据账单日获取首期还款日
     * @param paymentDay
     * @param statementDay
     * @param loanDate
     * @return
     */
    Date getFirstPaymentDate(int paymentDay, int statementDay, Date loanDate);

    /**
     * 借款记录查询
     * @param req
     * @return
     */
    LoanListQueryRes loanListQuery(LoanListQueryReq req) throws TxnBizException;
}
